//===----------------------------------------------------------------------===//
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2024 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Soto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

// THIS FILE IS AUTOMATICALLY GENERATED by https://github.com/soto-project/soto-codegenerator.
// DO NOT EDIT.

#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif
@_spi(SotoInternal) import SotoCore

extension ApiGatewayManagementApi {
    // MARK: Enums

    // MARK: Shapes

    public struct DeleteConnectionRequest: AWSEncodableShape {
        public let connectionId: String

        @inlinable
        public init(connectionId: String) {
            self.connectionId = connectionId
        }

        public func encode(to encoder: Encoder) throws {
            let request = encoder.userInfo[.awsRequest]! as! RequestEncodingContainer
            _ = encoder.container(keyedBy: CodingKeys.self)
            request.encodePath(self.connectionId, key: "ConnectionId")
        }

        private enum CodingKeys: CodingKey {}
    }

    public struct GetConnectionRequest: AWSEncodableShape {
        public let connectionId: String

        @inlinable
        public init(connectionId: String) {
            self.connectionId = connectionId
        }

        public func encode(to encoder: Encoder) throws {
            let request = encoder.userInfo[.awsRequest]! as! RequestEncodingContainer
            _ = encoder.container(keyedBy: CodingKeys.self)
            request.encodePath(self.connectionId, key: "ConnectionId")
        }

        private enum CodingKeys: CodingKey {}
    }

    public struct GetConnectionResponse: AWSDecodableShape {
        /// The time in ISO 8601 format for when the connection was established.
        @OptionalCustomCoding<ISO8601DateCoder>
        public var connectedAt: Date?
        public let identity: Identity?
        /// The time in ISO 8601 format for when the connection was last active.
        @OptionalCustomCoding<ISO8601DateCoder>
        public var lastActiveAt: Date?

        @inlinable
        public init(connectedAt: Date? = nil, identity: Identity? = nil, lastActiveAt: Date? = nil) {
            self.connectedAt = connectedAt
            self.identity = identity
            self.lastActiveAt = lastActiveAt
        }

        private enum CodingKeys: String, CodingKey {
            case connectedAt = "connectedAt"
            case identity = "identity"
            case lastActiveAt = "lastActiveAt"
        }
    }

    public struct Identity: AWSDecodableShape {
        /// The source IP address of the TCP connection making the request to API Gateway.
        public let sourceIp: String?
        /// The User Agent of the API caller.
        public let userAgent: String?

        @inlinable
        public init(sourceIp: String? = nil, userAgent: String? = nil) {
            self.sourceIp = sourceIp
            self.userAgent = userAgent
        }

        private enum CodingKeys: String, CodingKey {
            case sourceIp = "sourceIp"
            case userAgent = "userAgent"
        }
    }

    public struct PostToConnectionRequest: AWSEncodableShape {
        /// The identifier of the connection that a specific client is using.
        public let connectionId: String
        /// The data to be sent to the client specified by its connection id.
        public let data: AWSHTTPBody?

        @inlinable
        public init(connectionId: String, data: AWSHTTPBody? = nil) {
            self.connectionId = connectionId
            self.data = data
        }

        public func encode(to encoder: Encoder) throws {
            let request = encoder.userInfo[.awsRequest]! as! RequestEncodingContainer
            var container = encoder.singleValueContainer()
            request.encodePath(self.connectionId, key: "ConnectionId")
            try container.encode(self.data)
        }

        public func validate(name: String) throws {
            try self.validate(self.data, name: "data", parent: name, max: 131072)
        }

        private enum CodingKeys: CodingKey {}
    }
}

// MARK: - Errors

/// Error enum for ApiGatewayManagementApi
public struct ApiGatewayManagementApiErrorType: AWSErrorType {
    enum Code: String {
        case forbiddenException = "ForbiddenException"
        case goneException = "GoneException"
        case limitExceededException = "LimitExceededException"
        case payloadTooLargeException = "PayloadTooLargeException"
    }

    private let error: Code
    public let context: AWSErrorContext?

    /// initialize ApiGatewayManagementApi
    public init?(errorCode: String, context: AWSErrorContext) {
        guard let error = Code(rawValue: errorCode) else { return nil }
        self.error = error
        self.context = context
    }

    internal init(_ error: Code) {
        self.error = error
        self.context = nil
    }

    /// return error code string
    public var errorCode: String { self.error.rawValue }

    /// The caller is not authorized to invoke this operation.
    public static var forbiddenException: Self { .init(.forbiddenException) }
    /// The connection with the provided id no longer exists.
    public static var goneException: Self { .init(.goneException) }
    /// The client is sending more than the allowed number of requests per unit of time or the WebSocket client side buffer is full.
    public static var limitExceededException: Self { .init(.limitExceededException) }
    /// The data has exceeded the maximum size allowed.
    public static var payloadTooLargeException: Self { .init(.payloadTooLargeException) }
}

extension ApiGatewayManagementApiErrorType: Equatable {
    public static func == (lhs: ApiGatewayManagementApiErrorType, rhs: ApiGatewayManagementApiErrorType) -> Bool {
        lhs.error == rhs.error
    }
}

extension ApiGatewayManagementApiErrorType: CustomStringConvertible {
    public var description: String {
        return "\(self.error.rawValue): \(self.message ?? "")"
    }
}
